package com.kuiandjie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kuiandjie.entity.FileType;
import com.kuiandjie.util.DBHelp;
import com.kuiandjie.util.RowMapper;

public class FileTypeDao {
	private DBHelp<FileType> db = new DBHelp<FileType>();
	
	private class FileTypeRowMapper implements RowMapper<FileType>{

		@Override
		public FileType mapperRow(ResultSet rs) throws SQLException {
			FileType ft = new FileType();
			ft.setCreateTime(rs.getString("createtime"));
			ft.setEmployeeId(rs.getInt("t_employee_id"));
			ft.setName(rs.getString("name"));
			ft.setProjectId(rs.getInt("t_project_id"));
			ft.setEmpName(rs.getString("empname"));
			ft.setId(rs.getInt("id"));
			return ft;
		}
		
	}

	public List<FileType> findByEmpIdAndProId( int proid) {
		String sql = "SELECT t_filetype.id,t_filetype.name,createtime,t_employee_id,t_project_id,t_employee.name AS empname FROM t_filetype LEFT JOIN t_employee ON t_filetype.t_employee_id = t_employee.id WHERE  t_project_id = ? ORDER BY createtime DESC";
		
		return db.executeQueryForList(sql, new FileTypeRowMapper() ,proid);
	}

	public void addFloder(FileType ft) {
		String sql = "INSERT INTO t_filetype(createtime,t_employee_id,t_project_id,name) VALUES(?,?,?,?)";
		db.executeSQL(sql, ft.getCreateTime(),ft.getEmployeeId(),ft.getProjectId(),ft.getName());
		
	}

	public FileType findFTByFTId(String folderid) {
		String sql = "SELECT id,name,name AS empname,createtime,t_employee_id,t_project_id FROM t_filetype WHERE id = ?";
		
		return db.executeQueryForObject(sql, new FileTypeRowMapper(), folderid);
	}

}
